#!/bin/env bash
# 编译安装nginx-1.18.0
# 判断是否为root用户，不是root用户则退出
if [ ${USER} != "root" ]; then
	echo "权限不足"
	exit
else
	echo "欢迎光~~临！"
fi

packages_dir="/usr/local/src/tar_packages"
install_dir="/usr/local/nginx"
nginx="nginx-1.18.0"
nginx_url="10.10.0.12/packages/nginx-1.18.0.tar.gz"

# 配置yum源
if [ -f /etc/yum.repos.d/CentOS-Base.repo -a -f /etc/yum.repos.d/epel.repo ]; then
	echo "镜像存在"
else	
	rm -rf /etc/yum.repos.d/*
	curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	yum makecache
fi

nginx_install(){

# 安装依赖
yum -y groupinstall "Development Tools"
yum -y install libxml2 libxml2-devel libxslt-devel automake zlib zlib-devel pcre pcre-devel openssl openssl-devel gd gd-devel geoip-devel
yum -y upgrade

# 准备软件包
if [ -d ${install_dir} ]; then
    echo "nginx已安装"
    exit
else
    echo "开始安装nginx"
fi

if [ -f ${packages_dir}/${nginx}.tar.gz ]; then
    echo "软件包已存在，不重复下载"
else
    wget -P ${packages_dir} ${nginx_url}
fi

tar xzvf ${packages_dir}/${nginx}.tar.gz -C ${packages_dir}/..
cd ${packages_dir}/../${nginx}
./configure --prefix=/usr/local/nginx --with-select_module --error-log-path=/usr/local/nginx/logs/error.log --user=nginx --group=nginx --http-log-path=/usr/local/nginx/logs/access.log --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_xslt_module=dynamic --with-http_image_filter_module --with-http_image_filter_module=dynamic --with-http_geoip_module --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-compat --with-pcre && make && make install

if [ $? -eq 0 ]; then
    echo "安装成功，进行下一步配置"
else
    echo "编译安装过程有错误，请排查原因后重新运行"
    exit
fi

groupadd nginx
useradd nginx -g nginx

# 添加环境变量和软链接
echo '$PATH:PATH=/usr/local/nginx/sbin' >> /etc/profile
source /etc/profile
ln -s ${install_dir}/sbin/nginx /usr/bin
cd /usr/lib/systemd/system/
cat > nginx.service << -EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
-EOF
systemctl start nginx
if [ $? -eq 0 ]; then
    echo "nginx启动成功"
    ss -ntlp | grep 80
    ps -ef | grep nginx
    systemctl enable nginx
else
    echo "nginx启动失败，请查看日志"
    exit
fi
}

nginx_install